#include "exp_1.h"
Course courseList;
int main() {
    int op;
    DataBase db;
    //载入数据库
    FILE *f;
    f = fopen("data.txt", "r");
    int len;
    //录入课程长度和学生人数
    fscanf(f, "%d %d", &db.courseNum, &len);
    Course *course = &db.courseList;
    Score *score;
    //录入课程名称，创建课程名称链表
    for (int i = 0; i < db.courseNum; ++i) {
        course->next = (Course *)malloc(sizeof(Course));
        course = course->next;
        fscanf(f, "%s", course->name);
    }
    course->next = NULL; //勿忘
    SqList_init_capacity(&db.studentList, len);
    //录入学生信息
    for (int i = 0; i < len; ++i) {
        db.studentList.data[i].id = i;
        fscanf(f, "%s", db.studentList.data[i].name);
        score = &db.studentList.data[i].scoreList;
        //录入该学生成绩
        for (int j = 0; j < db.courseNum; ++j) {
            score->next = (Score *)malloc(sizeof(Score));
            score = score->next;
            //%d前面有空格，忽略多个空格
            fscanf(f, " %d", &score->val);
        }
        score->next = NULL;
    }
    db.studentList.size = len;
    fclose(f);
    //界面
    while (1) {
        printf("欢迎使用计算机学院的2018级本科生的学生成绩管理\n");
        printf("1   显示学生信息列表\n");
        printf("2   显示学生成绩列表\n");
        printf("3   按学号查找学生信息\n");
        printf("4   按姓名查找学生信息\n");
        printf("5   按学号查找学生成绩\n");
        printf("6   按学号修改学生成绩\n");
        printf("7   增加一门科目\n");
        printf("8   删除一门科目\n");
        printf("9   退出系统\n");
        printf("请输入操作序号：");
        scan_i(&op);
        switch (op) {
        case 1: {
            SqList_traverse(&db.studentList, print_STU);
            break;
        }
        case 2: {
            for (int i = 0; i < db.studentList.size; ++i) {
                print_STU_Score(&db.studentList.data[i], &db.courseList);
            }
        }
        case 3: {
            printf("请输入学号：");
            int flag = 0; //看是否找到该学生
            int i = 0;
            SqList *L = &db.studentList;
            int student_num;      //学生学号
            char course_name[20]; //科目名称
            scanf_s("%d", &student_num);
            for (i = 0; i < len; i++) {
                if (L->data[i].id == student_num) {
                    flag = 1;
                }
            }
            if (flag == 0) {
                printf("未找到这个同学\n");
            }
            if (flag == 1) {
                {
                    print_STU_Score(&db.studentList.data[i], &db.courseList);
                }
            }
            break;
        }
        case 4: {
            printf("请输入姓名：");
            int flag = 0; //看是否找到该学生
            SqList *L = &db.studentList;
            int i = 0;
            char stu_name[21];
            char course_name[21]; //科目名称
            scanf_s("%s", stu_name);
            for (i = 0; i < len; i++) {
                if (strcmp(L->data[i].name, stu_name) == 0) {
                    flag = 1;
                }
            }
            if (flag == 0) {
                printf("未找到这个同学\n");
            }
            if (flag == 1) {
                {
                    print_STU_Score(&db.studentList.data[i], &db.courseList);
                }
            }
            break;
        }
        case 5: {
            printf("请输入学号：");
            SqList *L = &db.studentList;
            int flag = 0; //看是否找到该学生
            int i = 0;
            int student_num;      //学生学号
            char course_name[20]; //科目名称
            scanf_s("%d", &student_num);
            for (i = 0; i < len; i++) {
                if (L->data[i].id == student_num) {
                    flag = 1;
                }
            }
            if (flag == 0) {
                printf("未找到这个同学\n");
            }
            if (flag == 1) {
                {
                    printf("------------------------------\n");
                    Score *sc = &db.studentList.data[i].scoreList;
                    Course *co = &db.courseList;
                    while (co->next) {
                        co = co->next;
                        sc = sc->next;
                        printf("%s：%d\t", co->name, sc->val);
                    }
                    printf("\n------------------------------\n");
                }
            }
            break;
        }
        case 6: {
            modified_grades(&db.studentList, len, &db.courseList); //修改成绩
            break;
        }
        case 7: {
            add_subject(&db, len); //增加课程
            break;
        }
        case 8: {
            dele_subject(&db, len); //删除课程
            break;
        }
        case 9: {
            return 0;
            break;
        }
        }
    }
    return 0;
}

//函数定义
void add_subject(DataBase *L, int len) //增加课程
{
    Course *co = &L->courseList;
    Score *f;
    int i = 0;
    int j = 0;
    for (i = 0; co->next != NULL; i++) {
        co = co->next;
    }
    co->next = (Course *)malloc(sizeof(Course));
    co = co->next;
    printf("请输入要添加的课程名称\n");
    scanf("%s", co->name);
    co->next = NULL;
    L->courseNum++;
    for (j = 0; j < len; j++) //每一个学生的课程表增加一门课程
    {
        f = &L->studentList.data[j].scoreList;
        while (i > 0) //找到最后一位
        {
            f = f->next;
            i--;
        }
        f->next == (Score *)malloc(sizeof(Score));
        f = f->next;
        f->next = NULL;
        f->val = -1;
    }
}
void dele_subject(DataBase *L, int len) //删除课程
{
    int i, j = 0;
    int k = 0;
    char name[20];
    Course *co = &L->courseList;
    Course *p = &L->courseList;
    Score *s;
    printf("请输入要删除的科目名称\n");
    scanf("%s", name);
    for (i = 0; co->next != NULL; i++) {
        if (strcmp(co->next->name, name) == 0) {
            p = co->next;
            co->next = p->next;
            free(p);
            break;
        }

        co = co->next;
    }
    for (j = 0; j < len; j++) {
        s = &L->studentList.data[j].scoreList;
        while (k < (i + 1)) {
            s = s->next;
            k++;
        }
        s->val = -1;
    }
}
void modified_grades(SqList *L, int len, Course *co) //修改学生某一科目成绩
{
    int flag = 0; //看是否找到该学生
    int i = 0;
    int student_num;      //学生学号
    char course_name[21]; //科目名称
    printf("输入要修改成绩的学生的学号\n");
    scanf_s("%d", &student_num);
    for (i = 0; i < len; i++) {
        if (L->data[i].id == student_num) {
            flag = 1;
            printf("找到该学生，请输入要修改成绩的科目名称\n");
            scanf("%s", course_name);
            Score *s = &L->data[i].scoreList; // s指向该学生的成绩链表的头结点
            while (co->next) {
                if (strcmp(co->name, course_name) == 0) {
                    printf("找到该科目，请输入要改的成绩\n");
                    scanf_s("%d", &(s->val));
                    break;
                }
                co = co->next;
                s = s->next;
            }
            break;
        }
    }
    if (flag == 0) {
        printf("未找到这个同学\n");
    }
}